import { createResource } from 'solid-js';
import { getAllCategories, getCategoryApps } from '@/lib/api/category';
import type { Category } from '@/types/category';
import { AppItem } from '@/types/app';

const fetchCategories = async (): Promise<Category[]> => {
  return await getAllCategories();
};

export const fetchCategoryApps = async (categoryId: string): Promise<AppItem[]> => {
  return await getCategoryApps(categoryId);
};

export const useCategoriesStore = () => {
  const [categories, { refetch }] = createResource<Category[]>(fetchCategories);
  const loading = () => categories.loading;

  return {
    categories,
    loading,
    refetch,
  };
};